package tr.hba.jblog.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tr.hba.jblog.model.User;

@Repository
public class UserDAO implements IUserDAO {

	@Autowired(required = true)
	@PersistenceContext
	protected EntityManager entityManager;

	public void create(User entity) {
		entityManager.persist(entity);

	}

	public void delete(User entity) {
		entityManager.remove(entity);

	}

	@SuppressWarnings("unchecked")
	public List<User> loadAll() {
		return entityManager.createQuery("Select e from User e")
				.getResultList();
	}

	public User loadById(Long id) {
		return entityManager.find(User.class, id);
	}

	public void update(User entity) {
		entityManager.merge(entity);

	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

}
